package com.frank.demo.io.netty.echo;

import com.frank.demo.io.netty.http.NettyServer;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.ByteToMessageCodec;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.util.CharsetUtil;
import org.apache.rocketmq.common.ThreadFactoryImpl;

import java.net.InetSocketAddress;
import java.util.List;

/**
 * Created by Administrator on 2018/10/14 0014.
 */
public class EchoServer {


    public static void main(String[] args) throws InterruptedException {
        EchoServer server = new EchoServer();
        server.start();
    }

    private void start() throws InterruptedException {
//        NettyServer server = new NettyServer();
        ServerBootstrap server = new ServerBootstrap();

        NioEventLoopGroup boss = new NioEventLoopGroup(1, new ThreadFactoryImpl("boss@"));
        NioEventLoopGroup worker = new NioEventLoopGroup(0, new ThreadFactoryImpl("worker@"));

        server.group(boss, worker)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer() {
                    @Override
                    protected void initChannel(Channel ch) throws Exception {
                        ch.pipeline()
                                .addLast(new LineBasedFrameDecoder(80))
                                .addLast(new ByteToMessageDecoder() {
                                    @Override
                                    protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception {
                                        String str;
                                        byte[] b = new byte[buf.readableBytes()];
                                        buf.readBytes(b);
                                        str = new String(b);
                                        out.add(str);
                                    }
                                })
                                .addLast(new MessageToByteEncoder<String>() {
                                    @Override
                                    protected void encode(ChannelHandlerContext ctx, String msg, ByteBuf out) throws Exception {
                                        out.writeBytes(msg.getBytes());
                                    }
                                })
                                .addLast(new ChannelInboundHandlerAdapter() {
                                    @Override
                                    public void channelActive(ChannelHandlerContext ctx) throws Exception {
                                        System.out.println("connect success");
                                    }

                                    @Override
                                    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                        System.out.println(msg);
                                        ctx.writeAndFlush(Unpooled.copiedBuffer("Netty test", CharsetUtil.UTF_8));
                                        ctx.close();

                                    }

                                    @Override
                                    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
                                        super.channelReadComplete(ctx);
                                    }

                                    @Override
                                    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
                                        super.exceptionCaught(ctx, cause);
                                        System.out.println("errror");
                                    }


                                    @Override
                                    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
                                        super.channelUnregistered(ctx);
                                        System.out.println("unregister");
                                    }
                                });
                    }
                })
                .bind(new InetSocketAddress("127.0.0.1", 7777))
                .sync();


    }


}
